// pages/bindUserInfo/bindUserInfo.js
const app = getApp();
let tool = require('../../utils/tool.js');
const httpUtils = require('../../utils/httpUtils.js');
let util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBoy: 1,
    isGirl: 0,
    gradeId: 0, //年级
    isSelectGrade: "点击选择年级",
    gradeId: '',
    phone: "",
    levelData: null,
    order_no:""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      phone:options.phone,
      order_no:options.order_no
    })

    this.getgrade();


  },

  //节流函数，防止登录按钮快点
  formSubmit: util.throttle(function (e) {

    let that = this;
    var name = e.detail.value.uname;
    var school = e.detail.value.school;
    if (that.data.gradeId == 0) {
      wx.showToast({
        title: "请选择年级",
        icon: 'none',
        duration: 2000
      })
      return
    }
  
    if (name == "") {
      wx.showToast({
        title: "请输入姓名",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (school == "") {
      wx.showToast({
        title: "请输入学校",
        icon: 'none',
        duration: 2000
      })
      return
    }
  
   
    wx.showLoading({
      title: '正在加载',
      mask: true
    })

    this.setData({
      school: school,
      name: name,
    })
    this.refer()
  }, 1500),

  refer: function () {
  let that=this
    httpUtils.bindaccount({
      access_token: app.globalData.$TOKEN,
      school: that.data.school,
      nickname: that.data.name,
      sex: that.data.mIsBoy == 1 ? "1" : "2",
      gradeid: that.data.gradeId,
      order_no: that.data.order_no,
    }).then((res) => {
      wx.showToast({
        title: "已完善",
        icon: 'none',
        duration: 2000
      })
      wx.navigateBack({
        delta: 1,
      })
    })
  },
  selectBoy: function () {
    let that = this
    that.setData({
      isBoy: 1,
      isGirl: 0
    })
  },
  selectGirl: function () {
    let that = this
    that.setData({
      isBoy: 0,
      isGirl: 1

    })
  },
 

  
  getgrade: function () {
    let that = this;
    httpUtils.getgrade({
      access_token: app.globalData.$TOKEN,
    }).then((res) => {
      that.setData({
        levelData: res.data
      })

    })
  },
  
  bindPickerChange: function (e) {
    let that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      isSelectGrade: that.data.levelData[e.detail.value].grade_name,
      gradeId: that.data.levelData[e.detail.value].grade
    })
  },
 
  


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})